package com.jumi.microservice.stock.factory;

import com.jumi.microservice.domain.GoodsWarehouseStockDO;
import com.jumi.microservice.mapper.GoodsWarehouseStockMapper;
import com.jumi.microservice.stock.StockUpdater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存更新命令工厂的父类
 *
 * @author Administrator
 */
public abstract class AbstractStockUpdaterFactory<T> implements StockUpdaterFactory<T> {

    private static final Logger logger = LoggerFactory.getLogger(AbstractStockUpdaterFactory.class);

    /**
     * 商品库存管理模块的mapper组件
     */
    protected GoodsWarehouseStockMapper goodsWarehouseStockMapper;

    /**
     * 构造函数
     *
     * @param goodsStockMapper 商品库存管理模块的mapper组件
     */
    public AbstractStockUpdaterFactory(GoodsWarehouseStockMapper goodsStockMapper) {
        this.goodsWarehouseStockMapper = goodsStockMapper;
    }

    /**
     * 创建库存更新命令
     */
    @Override
    public StockUpdater create(T parameter) {
        try {
            List<Long> goodsSkuIds = getGoodsSkuIds(parameter);
            List<GoodsWarehouseStockDO> goodsStocks = createGoodsStocks(goodsSkuIds);
            return create(goodsStocks, parameter);
        } catch (Exception e) {
            logger.error("error", e);
        }
        return null;
    }

    /**
     * 获取商品sku id集合
     *
     * @param parameter 参数
     * @return 商品sku id集合
     * @throws Exception
     */
    protected abstract List<Long> getGoodsSkuIds(T parameter) throws Exception;

    /**
     * 创建库存更新命令
     *
     * @param parameter   参数
     * @param goodsStocks 商品库存DO对象集合
     * @return 库存更新命令
     * @throws Exception
     */
    protected abstract StockUpdater create(List<GoodsWarehouseStockDO> goodsStocks, T parameter) throws Exception;

    /**
     * 创建商品库存DO对象集合
     *
     * @param goodsSkuIds 商品sku id集合
     * @return 商品库存DO对象集合
     */
    private List<GoodsWarehouseStockDO> createGoodsStocks(List<Long> goodsSkuIds) throws Exception {
        List<GoodsWarehouseStockDO> goodsStocks = new ArrayList<>(goodsSkuIds.size());

        for (Long goodsSkuId : goodsSkuIds) {
            // GoodsStock goodsStock = goodsStockMapper.getGoodsStockBySkuId(goodsSkuId);
            GoodsWarehouseStockDO goodsStock = null;
            if (goodsStock == null) {
                goodsStock = createGoodsStock(goodsSkuId);
                goodsWarehouseStockMapper.insert(goodsStock);
            }
            goodsStocks.add(goodsStock);
        }

        return goodsStocks;
    }

    /**
     * 创建商品库存DO对象
     *
     * @param goodsSkuId 商品sku id
     * @return 商品库存DO对象
     * @throws Exception
     */
    private GoodsWarehouseStockDO createGoodsStock(Long goodsSkuId) throws Exception {
        GoodsWarehouseStockDO goodsStockDO = new GoodsWarehouseStockDO();
        goodsStockDO.setGoodsSkuId(goodsSkuId);
        goodsStockDO.setSaleStockQuantity(0);
        goodsStockDO.setLockedStockQuantity(0);
        goodsStockDO.setSaledStockQuantity(0);
        goodsStockDO.setCreateTime(LocalDateTime.now());
        goodsStockDO.setUpdateTime(LocalDateTime.now());
        return goodsStockDO;
    }

}
